//编辑时将值带入
function setEditData () {
    if (Object.keys(parent.EditData).length > 0) {
        var form = document.getElementById("entryForm");
        for (var i = 0; i < form.elements.length; i++) {
            console.log(parent.EditData, form.elements[i].name)
            form.elements[i].value = parent.EditData[form.elements[i].name] || '';//elements:form接管的input
        }
    }
}

$(document).ready(setEditData)


//点击返回book页
var navRight = document.getElementsByClassName('nav-right')[0];
navRight.onclick = function pageJump() {
    parent.EditData = {};
    var url = location.href;
    url = url.substring(0, url.lastIndexOf("/"));
    window.location.href = `${url}/dxm-index.html`;
}
var cancelAddBook = document.getElementsByClassName('cancel-addbook')[0];
cancelAddBook.onclick = function pageJump() {
    parent.EditData = {};
    var url = location.href;
    url = url.substring(0, url.lastIndexOf("/"));
    window.location.href = `${url}/dxm-index.html`;
}
//上传图片
function fileUpload() {
    var files = document.getElementById("input").files;
    var form = new FormData(),
        file = files[0];
    form.append('file', file);
    var reader = new FileReader();
    reader.readAsDataURL(file); //base64
    //接收到图片时触发onload
    reader.onload = function(e){
        var result = reader.result;
        var img = document.createElement("img");
        img.src = result;
        img.id = "showImg";
        if ($("#showImg")) {
            $("#showImg").remove();
        }
        $(img).insertBefore($("#input"));
        /*document.getElementById(imgId).src = result;
        document.getElementById(imgId).classList.remove('hide');
        document.getElementById(addId).classList.add('hide');
        document.getElementById(closeId).classList.remove('hide');*/
    };
}



var books = [];
//初始化
function init() {
    getbookApi();
}
//请求图书信息
function getbookApi(){

    $.ajax({
        url: '/books/add',
        method: 'GET',
    })
        .done(function (res) {
            books = res.data()
        })
        .fail(function (err) {
            console.log('请求失败：', err);
        });
}
//提交数据
function addBooks() {
    var form = document.getElementById("entryForm");
    var formData = new FormData(form);//实例化一个构造函数
    var date = new Date();
    if(bookId) {
        submitEditBook();
    }else {
        submitAddBook();
    }
    if (parent.EditData && Object.keys(parent.EditData).length > 0) {
        editBooks(param);
    }else {//反之是空的，说明是新增的，就用新增的方法
        $.ajax({
            url: '/books/add',
            method: 'POST',
            data: {
                name: "1",
                subject: "小说",
                author: "11",
                publish: "232",
                price: 20,
                position: "111",
                time: `2021-3-10`,
            }
        })
            .done(function (res) {
                console.log(res);
            })
            .fail(function (err) {
                console.log('请求失败：', err);
            });
    }
}
// 获得图书信息
function getBookInfo() {
    var form = document.getElementById("entryForm");
    $.ajax({
        url:'/books/getById',
        method:'GET',
        data: {
            bookId
        }
    })
        .done(function(res) {
            const param = {
                id: Number(formData.get("id")),
                name: formData.get("name"),
                subject: "小说",
                author: formData.get("author"),
                publish: formData.get("publish"),
                price: Number(formData.get("price")),
                position: formData.get("position"),
                time: `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`,
                description: formData.get("description"),
            }
        })
        .fail(function(err) {
            console.log('请求图书详细信息失败：', err);
        })
}
// 添加
function submitAddBook() {
    var form = document.getElementById("entryForm");
    var formData = new FormData(form);//实例化一个构造函数
    var date = new Date();
    if (!(formData.get("name") && formData.get("id") && formData.get("description") && formData.get("author") && formData.get("publish") && formData.get("price") && formData.get("position"))) {
        alert("请完善信息");
        return;
    }
    const param = {
        id: Number(formData.get("id")),
        name: formData.get("name"),
        subject: "小说",
        author: formData.get("author"),
        publish: formData.get("publish"),
        price: Number(formData.get("price")),
        position: formData.get("position"),
        time: `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`,
        description: formData.get("description"),
    }
    $.ajax({
        url:'/courses/add',
        method:'POST',
        data:param
    })
        .done(function(res) {
            console.log(res.msg,'kkk',res.err)
        })
        .fail(function(err) {
            console.log(err.msg,'hhh',err.err)
        })
}

// 编辑
function submitEditBook() {
    var form = document.getElementById("entryForm");
    var formData = new FormData(form);//实例化一个构造函数
    var date = new Date();
    const param = {
        id: Number(formData.get("id")),
        name: formData.get("name"),
        subject: "小说",
        author: formData.get("author"),
        publish: formData.get("publish"),
        price: Number(formData.get("price")),
        position: formData.get("position"),
        time: `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`,
        description: formData.get("description"),
    }
    $.ajax({
        url:'/courses/edit',
        method:'POST',
        data:param
    })
        .done(function(res) {
            console.log(res.msg,'bbb',res.err)
        })
        .fail(function(err) {
            console.log(err.msg,err.err)
        })
}





